package com.yanqu.road.server.cmd.children;

import com.yanqu.road.entity.config.children.ChildTalentInfo;
import com.yanqu.road.entity.config.children.ChildTrainInfo;
import com.yanqu.road.entity.player.UserChildSite;
import com.yanqu.road.entity.servercenter.GameEditLimit;
import com.yanqu.road.pb.child.ChildProto;
import com.yanqu.road.server.GameServer;
import com.yanqu.road.server.cmd.base.AbstractCommand;
import com.yanqu.road.server.gameplayer.GamePlayer;
import com.yanqu.road.server.gameplayer.module.player.ChildModule;
import com.yanqu.road.server.manger.ForbiddenWordMgr;
import com.yanqu.road.server.manger.GameEditLimitMgr;
import com.yanqu.road.server.manger.NameMgr;
import com.yanqu.road.server.manger.ServerListMgr;
import com.yanqu.road.server.manger.config.ChildMgr;
import com.yanqu.road.message.YanQuMessage;
import com.yanqu.road.server.protocol.GameErrorCode;
import com.yanqu.road.server.protocol.Protocol;
import com.yanqu.road.server.command.Cmd;
import com.yanqu.road.utils.string.StringUtils;

import java.util.Objects;

@Cmd(code = Protocol.S_CHILD_CHANGE_NAME, desc = "子嗣赐名")
public class ChildChangeNameCmd extends AbstractCommand {

    private int siteIndex;

    private String childUid;

    private String name;

    private String name2;

    private int trainType;

    private boolean openComprehend;

    @Override
    public boolean parseData(YanQuMessage packet) throws Exception {
        ChildProto.ChildChangeNameReqMsg reqMsg = ChildProto.ChildChangeNameReqMsg.parseFrom(packet.getBytes());
        siteIndex = reqMsg.getSiteIndex();
        childUid = reqMsg.getChildUid();
        name = reqMsg.getName();
        name2 = reqMsg.getName2();
        trainType = reqMsg.getTrainType();
        if(reqMsg.hasOpenComprehend()){
            openComprehend = reqMsg.getOpenComprehend();
        }
        return true;
    }

    @Override
    public void execute(GamePlayer player, YanQuMessage packet) throws Exception {
        ChildModule childModule = player.getModule(ChildModule.class);
        UserChildSite userChildSite = childModule.getUserChildSite(siteIndex);
        if (null == userChildSite) {// 不存在该子女
            ret = GameErrorCode.E_CHILD_SITE_NO_FOUND;
            return;
        }

        if(!Objects.equals(userChildSite.getChildUid(), childUid)){
            ret = GameErrorCode.E_CHILD_SITE_HAS_CHANGE;
            return;
        }

        if(!StringUtils.isNullOrEmpty(userChildSite.getChildName())){
            ret = GameErrorCode.E_CHILD_HAS_NAME;
            return;
        }

        ChildTalentInfo childTalentInfo = ChildMgr.getChildTalentInfo(userChildSite.getChildTalent());
        if(openComprehend && childTalentInfo.getId() != 10012){
            //品质判断
            ret = GameErrorCode.E_CHILD_NO_FOUND;
            return;
        }

        ChildTrainInfo childTrainInfo = ChildMgr.getChildTrainInfo(trainType);
        if(null == childTrainInfo){
            ret = GameErrorCode.E_CHILD_TRAIN_TYPE_ERROR;
            return;
        }

        if(!ChildMgr.canUseChildTrainType(player.getTitleId(), trainType)){
            ret = GameErrorCode.E_CHILD_TRAIN_TYPE_CAN_NO_USE;
            return;
        }
        ret = checkName(name);
        if(ret != 0)return;
        if(userChildSite.getChildType() != 0){
            ret = checkName(name2);
            if(ret != 0)return;
        }

        //编辑限制
        GameEditLimit gameEditLimit = GameEditLimitMgr.getGameEditLimit(Protocol.S_CHILD_CHANGE_NAME);
        if(null != gameEditLimit){
            long nowTime = System.currentTimeMillis();
            if(nowTime >= gameEditLimit.getLimitEditStartTime() && nowTime < gameEditLimit.getLimitEditEndTime()){
                int sex1 = 0;
                int sex2 = 0;
                if(userChildSite.getChildType() > 0){
                    sex1 = ChildMgr.getChildSexByPosition(userChildSite, 1);
                    sex2 = ChildMgr.getChildSexByPosition(userChildSite, 2);
                }else {
                    sex1 = userChildSite.getChildSex();
                }
                String language = player.getLanguage();
                name = ChildMgr.getRandomChildName(sex1, language);
                if(sex2 > 0){
                    name2 = ChildMgr.getRandomChildName(sex2, language);
                }
            }
        }
        userChildSite.setChildName(name);
        userChildSite.setChildName2(name2);
        userChildSite.setTrainType(trainType);
        if(openComprehend) {
            userChildSite.setComprehend(true);
        }
        userChildSite.setTrainCostSilver(ChildMgr.getTrainCostSilver(player.getTitleId(), trainType, childModule.getChildSiteCount()));
        childModule.syncChildSiteData(userChildSite);
    }

    private int checkName(String name){
        if(StringUtils.isNullOrEmpty(name)){
            return GameErrorCode.E_CHILD_NAME_NO_EMPTY;
        }
        int nameLength = StringUtils.getStringLen(name);
        int channelId = ServerListMgr.getChannelId(GameServer.getInstance().getServerId());
        if(nameLength > NameMgr.getMaxNickNameLength(channelId) || nameLength < NameMgr.MIN_NICK_NAME_LENGTH){
            return GameErrorCode.E_NAME_LENGTH_OVER;
        }
        //敏感字过滤
        if(ForbiddenWordMgr.isNickNameForbidden(name)){
            return GameErrorCode.E_CHILD_NAME_FORBIDDEN;
        }
        return 0;
    }

    @Override
    public void sendData(GamePlayer player) {
        ChildProto.ChildChangeNameRespMsg.Builder respMsg = ChildProto.ChildChangeNameRespMsg.newBuilder();
        respMsg.setRet(ret);
        player.sendPacket(Protocol.U_CHILD_CHANGE_NAME, respMsg);
    }
}
